﻿using Game.Actions;
using Game.Command;
using Game.Common;
using Game.Level;
using Game.Map;
using Game.Map.Extensions;
using Game.Objects;

namespace Game.Controller.Internal
{
    class PlayerHandler : TurnHandler
    {
        public override void OnStartTurn(
            Vehicle vehicle, 
            IGameMapView gameMap,
            GameLevels levels,
            ActionExecutor actionExecutor,
            GameController controller,
            GameTurn gameTurn)
        {
        }
        
        public override void OnClick(
            ActionExecutor actionExecutor,
            IMapFieldView mapField,
            IGameMapView gameMap,
            GameLevels levels,
            GameTurn gameTurn,
            GameCommand cmd)
        {
            if (cmd != null) 
            {
                actionExecutor.Execute(gameMap, levels, gameTurn, cmd);
                ClearCommands(gameMap);
            } 
            else 
            {
                ClearCommands(gameMap);
                if (mapField.VehicleX().Vehicle != null) 
                {
                    if (mapField.VehicleX().Vehicle.Owner == HumanPlayer.Player)
                    {
                        {
                            var stratagy = new MoveStatagy(gameMap);
                            stratagy.AssignCommand(mapField, gameTurn);
                        }
                        {
                            var stratagy = new AttackStratagy(gameMap);
                            stratagy.AssignCommand(mapField);
                        }
                        {
                            var stratagy = new CaptureStratagy(gameMap);
                            stratagy.AssignCommand(mapField);
                        }
                    }
                }
            }
        }
        
        void ClearCommands(IGameMap gameMap)
        {
            foreach (var field in gameMap.Fields)
            {
                field.CommandX().Command = null;
                field.ViewX().GameImage = GameImage.None;
            }
        }
    }
}
